const multiparty = require("multiparty");

const dir = require("../CONST");
const multipart_file = (req, auto) => {
  // 地址不能写错，否则报错
  const uploadDir = `${dir}\\upload`;

  typeof auto !== "boolean" ? (auto = false) : null;
  const config = {
    maxFieldsSize: 200 * 1024 * 1024,
  };
  if (auto) config.uploadDir = uploadDir;
  return new Promise((resolve, reject) => {
    new multiparty.Form(config).parse(req, (err, fileds, files) => {
      if (err) {
        reject(err);
      } else {
        resolve({
          fileds,
          files,
        });
      }
    });
  });
};

module.exports = multipart_file;
